package com.example.test.mapper;

import com.example.test.entity.Image;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Delete;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Mapper
@Transactional
public interface ImageMapper {

    @Insert("INSERT INTO image (productId, path) VALUES (#{productId}, #{path})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertProductImage(Image image);

    @Select("SELECT path FROM image WHERE productId = #{productId}")
    List<String> getProductImages(Long productId);

    @Select("SELECT path FROM image WHERE id = #{imageId}")
    String getImageById(Long imageId);

    @Delete("DELETE FROM image WHERE id = #{imageId}")
    void deleteProductImage(Long imageId);

    @Delete("DELETE FROM image WHERE productId = #{productId}")
    void deleteProductImagesByProductId(Long productId);

    @Select("select *from image where productId=#{productId}")
    List<Image> getProductImagesByProductId(Long productId);
}

